﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Properties;
using System.Web.Routing;
using Smarly.Bussines.Model;
using Smarly.Web.ActionFilters;

namespace Smarly.Web.Helpers
{
	public static class HtmlHelperExtentions
	{
		public static string IsActive(this string val, string active, string onActive, string onNotActive = "")
		{
			if (string.IsNullOrWhiteSpace(val) || string.IsNullOrWhiteSpace(active))
			{
				return string.Empty;
			}

			return active.Trim() == val.Trim() ? onActive ?? string.Empty : onNotActive ?? string.Empty;
		}

		static Random r = new Random();
		public static T Random<T>(this List<T> list) where T : class
		{
			if (list == null || list.Count == 0)
				return null;
			return list[r.Next(list.Count)];
		}

		public static UrlHelper Url(this HtmlHelper helper)
		{
			return new UrlHelper(helper.ViewContext.RequestContext);
		}

		public static string ConvertRelativeUrlToAbsoluteUrl(this HtmlHelper htmlHelper, string relativeUrl)
		{
			return string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, relativeUrl);
		}

		public static bool IsDebug(this HtmlHelper htmlHelper)
		{
#if DEBUG
      return true;
#else
			return false;
#endif
		}

		public static IHtmlString PrevContentTop(this HtmlHelper htmlHelper, BookTree model)
		{
			string result = string.Empty;
			if (model.Parent != null)
			{
				int index = model.Parent.Children.IndexOf(model);

				if (index > 0)
				{
					var prev = model.Parent.Children[index - 1];
					result = string.Format("<div class=\"prev-content-top\"><a href='{0}'>{1}</a></div>", prev.Url, prev.Text);
				}
			}
			return new HtmlString(result);
		}

		public static IHtmlString NextContentBottom(this HtmlHelper htmlHelper, BookTree model)
		{
			string result = string.Empty;
			if (model.Parent != null)
			{
				int index = model.Parent.Children.IndexOf(model);

				if (index >= 0 && model.Parent.Children.Count > index + 1)
				{
					var next = model.Parent.Children[index + 1];
					result = string.Format("<div class=\"next-content-bottom\"><a href='{0}'>{1}</a></div>", next.Url, next.Text);
				}
			}
			return new HtmlString(result);
		}

		private static readonly MvcHtmlString EmptyMvcString = new MvcHtmlString(string.Empty);

		public static IHtmlString ValidationMessageCustomFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)
		{
			IHtmlString message = helper.ValidationMessageFor(expression) ?? EmptyMvcString;
			TagBuilder label = new TagBuilder("label"){InnerHtml = message.ToString()};
			label.AddCssClass("error");

			label.Attributes.Add("generated", "true");
			label.Attributes.Add("for", helper.IdFor(expression).ToString());

			return MvcHtmlString.Create(label.ToString(TagRenderMode.Normal));
		}
	}
}